package com.wy.service;


import com.wy.dao.ContactMapper;
import com.wy.pojo.Contact;
import com.wy.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class ContactService {
    public List<Contact> findAll() {
        //1.获取sqlSession对象
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        //2.获取dao接口的代理类对象
        ContactMapper mapper = sqlSession.getMapper(ContactMapper.class);
        //3.调用方法执行
        List<Contact> list = mapper.findAll();
        //4.关闭sqlSession
        sqlSession.close();
        return list;
    }
    public boolean addContact(Contact contact) {
        //获取SQLSession对象
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        ContactMapper mapper = sqlSession.getMapper(ContactMapper.class);
        int i = mapper.addContact(contact);
        sqlSession.commit();
        MybatisUtils.closeSqlSession(sqlSession);
        return i>0;
    }

    public Contact findContactById(String id) {
        //获取SQLSession对象
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        ContactMapper mapper = sqlSession.getMapper(ContactMapper.class);
        Contact contact = mapper.findContactById(id);
        return contact;
    }

    public boolean updateContact(Contact contact) {
        //获取SQLSession对象
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        ContactMapper mapper = sqlSession.getMapper(ContactMapper.class);
        int i = mapper.updateContact(contact);
        sqlSession.commit();
        MybatisUtils.closeSqlSession(sqlSession);
        return i>0;
    }

        public boolean deleteContactById(String id) {
            //获取SQLSession对象
            SqlSession sqlSession = MybatisUtils.getSqlSession();
            ContactMapper mapper = sqlSession.getMapper(ContactMapper.class);
            int i = mapper.deleteContactById(id);
            sqlSession.commit();
            MybatisUtils.closeSqlSession(sqlSession);
            return i>0;
        }

}
